﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain;
using Ue.Qcdz.Domain.Membership;
using Ue.Qcdz.Domain.ResourceManager;

namespace Ue.Qcdz.UI.Controls.Membership
{
    public class ForgetPwd : SkinnedControl
    {
        #region Overrides of SkinnedControl
        private string skinFileName = "~/Themes/zh-cn/Skins/UserForget.ascx";
        private TextBox tbxUserName;
        private Button btnSubmit;
        
        protected override string GetSkinFileName()
        {
            return skinFileName;
        }

        protected override void InitializeSkin(Control skin)
        {
            tbxUserName = skin.FindControl<TextBox>("tbxUserName");
            btnSubmit = skin.FindControl<Button>("btnSubmit");
            btnSubmit.Click += new EventHandler(btnSubmit_Click);
        }
        private void SendMailToChangePwd(string strUserName, string strUserKey)
        {
            string webSiteName = ResourceGlobals.Instance.GetString("Site_WebSite");
            string strUrl = string.Format("{0}/UserGetPassword.aspx?Id={1}&Key={2}&Mail={3}", webSiteName, strUserKey,
                                          Ue.Framework.Security.DEncryptUtils.MD5EnCode(strUserKey),Ue.Framework.Security.DEncryptUtils.MD5EnCode(strUserName));
            var emailBody = string.Format("<a href='{0}'>{0}</a>",strUrl);
            Email.SendMail(strUserName, "修改密码", emailBody);
            MessageWindow.RenderMessage("发送成功,请前往邮箱激修改密码");
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            string userName = tbxUserName.Text.Trim();
            IUserRepository repository = Ue.Infrastructure.RepositoryFramework.RepositoryFactory.GetRepository<IUserRepository, User>();
            User user = repository.FindBy(userName);
            if(user!=null)
            {
                SendMailToChangePwd(userName, user.Key.ToString());
            }
            else
            {
                this.MessageWindow.RenderMessage("用户不存在");
            }
        }

        #endregion
    }
}
